package com.gdue.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdue.commons.R;
import com.gdue.dto.DishDto;
import com.gdue.entity.Category;
import com.gdue.entity.Dish;
import com.gdue.service.CategoryService;
import com.gdue.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;
    
    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page>  pageR(int page,int  pageSize,String name){
        Page<Dish>    pageInfo=new Page<>(page,pageSize);

        Page<DishDto>   dishDtoPage=new Page<>();

        LambdaQueryWrapper<Dish>    lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        lambdaQueryWrapper.orderByAsc(Dish::getPrice);

        dishService.page(pageInfo,lambdaQueryWrapper);

        //对象拷贝                                                        忽略
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");

        List<Dish>  records=pageInfo.getRecords();

        //使用stream流进行赋值
        List<DishDto>   list=records.stream().map((item)->{
            DishDto dishDto=new DishDto();
            //数据拷贝
            BeanUtils.copyProperties(item,dishDto);
            //获取categoryId
            Long categoryId = item.getCategoryId();
            //根据categoryId查询数据
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            dishDto.setCategoryName(categoryName);
            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(list);

        return R.success(dishDtoPage);
    }

    /**
     * 新增菜品
     * @return
     */
    @PostMapping
    public R<String>  save(@RequestBody DishDto  dish){
        dishService.saveWithFlavor(dish);
        return R.success("菜品保存成功");
    }

    /**
     * 修改菜品前根据id查询菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto>  saveById(@PathVariable Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }

    /**
     * （批量）删除菜品
     * @return
     */
    @DeleteMapping
    public R<String>  delete(@RequestParam List<Long> ids){
        dishService.deleteWithFlavor(ids);
        return R.success("删除成功");
    }

    /**
     * 修改菜品的销售状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatusByIds(@PathVariable int status,@RequestParam List<Long>  ids){
        dishService.updateStatusByIds(status,ids);
        return  R.success("修改成功");
    }

    /**
     * 新增套餐中的查询菜品
     * @return
     */
    @GetMapping("/list")
    public R<List<Dish>>  getById(Dish  dish){

        LambdaQueryWrapper<Dish>  lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        lambdaQueryWrapper.eq(Dish::getStatus,1);
        lambdaQueryWrapper.orderByAsc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(lambdaQueryWrapper);

        return R.success(list);
    }
}
